﻿using System;
using System.Web.UI;
using MTC.Common;
using MTC.Common.Enum;
using MTC.Web.Business;
using MTC.Web.Business.DTO;

namespace MTC.Web.Forms
{
    public partial class DriverEdit : System.Web.UI.Page
    {
        private DriverEntity _entity;
        Business.Drivers DriversBO = new Business.Drivers();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                SetupEnvironment();
                //get the driver id if available
                if (Request.Params[Params.DRIVERID] != "0")
                {
                    int driverId = Convert.ToInt32(Request.Params[Params.DRIVERID]);
                    _entity = DriversBO.GetADriver(driverId);
                    LoadFields();
                }
            }
        }

        private void SetupEnvironment()
        {
            cboGender.DataSource = Gender.GetGender();
            cboGender.DataBind();
        }

        private void LoadFields()
        {
            if (_entity == null)
                return;
            hfDriverId.Value = _entity.DriverId.ToString();

            txtFirstName.Text = _entity.FirstName;
            txtLastName.Text = _entity.LastName;
            txtMobile1.Text  =_entity.Mobile1;
            txtMobile2.Text = _entity.Mobile2;
            txtABN.Text = _entity.ABN;
            txtDriverLicience.Text = _entity.Licience;
            txtETagAccount.Text = _entity.ETagAccount;
            txtETagNumber.Text = _entity.ETagNumber;
            txtNickName.Text = _entity.Nickname;
            txtTitle.Text = _entity.Title;
            cboGender.SelectedValue = _entity.Gender.ToString();
            txtStreet1.Text = _entity.Street1;
            txtStreet2.Text = _entity.Street2;
            txtSuburb.Text = _entity.Suburb;
            txtState.Text = _entity.State;
            txtPostcode.Text = _entity.Postcode;
            txtPhone.Text = _entity.Phone;
            txtTaxiLicience.Text = _entity.TaxiLicience;
            if (_entity.DateOfBirth.HasValue)
                txtDateofBirth.Text = Utils.ChangeFormat(_entity.DateOfBirth.Value);
            if (_entity.CommencementDate.HasValue)
                txtCommenceDate.Text = Utils.ChangeFormat(_entity.CommencementDate.Value);
            if (_entity.TaxiLicienceExpiry.HasValue)
                txtTaxiExpiryDate.Text = Utils.ChangeFormat(_entity.TaxiLicienceExpiry.Value);
            if (_entity.DriverLicienceExpiry.HasValue)
                txtDriverLicienceExpiryDate.Text = Utils.ChangeFormat(_entity.DriverLicienceExpiry.Value);


            txtMondayDayRate.Text = _entity.Rate.MonDayRate;
            txtMondayNightRate.Text = _entity.Rate.MonNightRate;
            txtTuesdayDayRate.Text = _entity.Rate.TuesDayRate;
            txtTuesdayNightRate.Text = _entity.Rate.TuesNightRate;
            txtWednesdayDayRate.Text = _entity.Rate.WedDayRate;
            txtWednesdayNightRate.Text = _entity.Rate.WedNightRate;
            txtThursdayDayRate.Text = _entity.Rate.ThursDayRate;
            txtThursdayNightRate.Text = _entity.Rate.ThursNightRate;
            txtFridayDayRate.Text = _entity.Rate.FriDayRate;
            txtFridayNightRate.Text = _entity.Rate.FriNightRate;
            txtSaturdayDayRate.Text = _entity.Rate.SatDayRate;
            txtSaturdayNightRate.Text = _entity.Rate.SatNightRate;
            txtSundayDayRate.Text = _entity.Rate.SunDayRate;
            txtSundayNightRate.Text = _entity.Rate.SunNightRate;

        }

        private void ClearFields()
        {
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtMobile1.Text = "";
            txtMobile2.Text = "";
            txtABN.Text = "";
            txtDriverLicience.Text = "";
            txtETagAccount.Text = "";
            txtETagNumber.Text = "";
            txtNickName.Text = "";
            txtTitle.Text = "";
            cboGender.SelectedIndex = 0;
            txtStreet1.Text = "";
            txtStreet2.Text = "";
            txtSuburb.Text = "";
            txtState.Text = "";
            txtPostcode.Text = "";
            txtPhone.Text = "";
            txtTaxiLicience.Text = "";
            txtDateofBirth.Text = "";
            txtCommenceDate.Text = "";
            txtTaxiExpiryDate.Text = "";
            txtDriverLicienceExpiryDate.Text = "";

            txtMondayDayRate.Text = "";
            txtMondayNightRate.Text = "";
            txtTuesdayDayRate.Text = "";
            txtTuesdayNightRate.Text = "";
            txtWednesdayDayRate.Text = "";
            txtWednesdayNightRate.Text = "";
            txtThursdayDayRate.Text = "";
            txtThursdayNightRate.Text = "";
            txtFridayDayRate.Text = "";
            txtFridayNightRate.Text = "";
            txtSaturdayDayRate.Text = "";
            txtSaturdayNightRate.Text = "";
            txtSundayDayRate.Text = "";
            txtSundayNightRate.Text = "";
        }

        private bool SetFields()
        {
            DriverEntity entity = new DriverEntity();
            DriverRateEntity rateEntity = new DriverRateEntity();

            if (hfDriverId.Value != "")
                entity.DriverId = Convert.ToInt32(hfDriverId.Value);

            entity.FirstName = txtFirstName.Text;
            entity.LastName = txtLastName.Text;
            entity.Mobile1 = txtMobile1.Text;
            entity.Mobile2 = txtMobile2.Text;
            entity.ABN = txtABN.Text;
            entity.Licience = txtDriverLicience.Text;
            entity.ETagAccount = txtETagAccount.Text;
            entity.ETagNumber = txtETagNumber.Text;
            entity.Nickname = txtNickName.Text;
            entity.Title = txtTitle.Text;
            entity.Gender = Convert.ToInt32(cboGender.SelectedItem.Value);
            entity.Street1 = txtStreet1.Text;
            entity.Street2 = txtStreet2.Text;
            entity.Suburb = txtSuburb.Text;
            entity.State = txtState.Text;
            entity.Postcode = txtPostcode.Text;
            entity.Phone = txtPhone.Text;
            entity.TaxiLicience = txtTaxiLicience.Text;
            if (txtDateofBirth.Text.Length > 0) 
                entity.DateOfBirth = Convert.ToDateTime(txtDateofBirth.Text);
            if (txtCommenceDate.Text.Length > 0)
                entity.CommencementDate = Convert.ToDateTime(txtCommenceDate.Text);
            if (txtTaxiExpiryDate.Text.Length > 0)
                entity.TaxiLicienceExpiry = Convert.ToDateTime(txtTaxiExpiryDate.Text);
            if (txtDriverLicienceExpiryDate.Text.Length > 0)
                entity.DriverLicienceExpiry = Convert.ToDateTime(txtDriverLicienceExpiryDate.Text);

            entity.Rate = rateEntity;
            entity.Rate.DriverId = entity.DriverId;
            entity.Rate.MonDayRate = txtMondayDayRate.Text;
            entity.Rate.MonNightRate = txtMondayNightRate.Text;
            entity.Rate.TuesDayRate = txtTuesdayDayRate.Text;
            entity.Rate.TuesNightRate = txtTuesdayNightRate.Text;
            entity.Rate.WedDayRate = txtWednesdayDayRate.Text;
            entity.Rate.WedNightRate = txtWednesdayNightRate.Text;
            entity.Rate.ThursDayRate = txtThursdayDayRate.Text;
            entity.Rate.ThursNightRate = txtThursdayNightRate.Text;
            entity.Rate.FriDayRate = txtFridayDayRate.Text;
            entity.Rate.FriNightRate = txtFridayNightRate.Text;
            entity.Rate.SatDayRate = txtSaturdayDayRate.Text;
            entity.Rate.SatNightRate = txtSaturdayNightRate.Text;
            entity.Rate.SunDayRate = txtSundayDayRate.Text;
            entity.Rate.SunNightRate = txtSundayNightRate.Text;
            
            if (entity.DriverId == 0)
            {
                DriversBO.AddDriver(entity);
            }
            else
            {
                return DriversBO.UpdateADriver(entity);                                
            }
            return true;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ValidateFields())
            {
                SetFields();
            }


        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearFields();
            Response.Redirect(Common.Urls.DriverListURL(), true);
        }

        protected bool ValidateFields()
        {
            return true;
        }

        protected void grdComments_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            e.NewValues["DriverId"] = Convert.ToInt32(Request.Params[Params.DRIVERID]);
        }

        protected void grdComments_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            e.NewValues["DriverId"] = Convert.ToInt32(Request.Params[Params.DRIVERID]);

        }

        protected void PageContents_ActiveTabChanged(object source, DevExpress.Web.ASPxTabControl.TabControlEventArgs e)
        {

        }
    }
}
